package com.yjd.bi.statistic.control;

import com.yjd.bi.comm.utils.BiDateUtil;
import com.yjd.bi.gift.service.IGiftReceiveService;
import com.yjd.bi.report.enums.TimeType;
import com.yjd.bi.report.model.BiDatabaseInfoModel;
import com.yjd.bi.report.model.EChartsModel;
import com.yjd.bi.report.service.IBiDatabaseInfoService;
import com.yjd.bi.report.util.BiUtil;
import com.yjd.comm.annotation.DateString;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by yt on 2016/12/16.
 */
@Controller
@Scope("prototype")
@RequestMapping("/giftReceive")
public class GiftReceiveControl extends BaseControl{
    //分享用户数
    public final static String share_user_count = "SELECT count(DISTINCT share_user_id) AS count FROM yjd_gift_receive WHERE start_time BETWEEN ? and ?";
    //领取用户数
    public final static String receive_user_count = "SELECT count(DISTINCT mobile) AS count FROM yjd_gift_receive WHERE start_time BETWEEN ? and ?";
    //新手机号
    public final static String new_mobile_count = "select count(DISTINCT(mobile)) from ( select * from yjd_gift_receive g1 where start_time < UNIX_TIMESTAMP('2016-12-01') and user_type = 1 union all select * from yjd_gift_receive g2 where start_time BETWEEN UNIX_TIMESTAMP('2016-12-01') and UNIX_TIMESTAMP('2016-12-16') and user_type = 4)a where a.start_time BETWEEN ? and ?";
    //注册用户数
    public final static String register_user_count = "select count(*) from yjd_gift_register_bind where regdate BETWEEN ? and ?";
    //首投用户数
    public final static String first_invest_count = "select count(DISTINCT(user_id)) from ( select min(i.addtime) as uptime, i.user_id from yjd_invest i ,yjd_gift_register_bind g where i.user_id = g.user_id group by i.user_id union select min(i.oktime) as uptime, i.buy_user_id as user_id from yjd_creditor_transfer i ,yjd_gift_register_bind g where i.buy_user_id = g.user_id group by i.buy_user_id) a where a.uptime BETWEEN ? and ?";
    //首投金额
    public final static String fisrt_invest_price = "SELECT ifnull(sum(price), 0.00) FROM ( SELECT min(uptime), price FROM ( SELECT * FROM ( SELECT min(i.addtime) AS uptime, i.price, i.user_id FROM yjd_invest i, yjd_gift_register_bind g WHERE i.user_id = g.user_id GROUP BY i.user_id UNION SELECT min(i.oktime) AS uptime, i.price, i.buy_user_id AS user_id FROM yjd_creditor_transfer i, yjd_gift_register_bind g WHERE i.buy_user_id = g.user_id GROUP BY i.buy_user_id ) t WHERE t.uptime BETWEEN ? AND ? ORDER BY uptime ASC ) s GROUP BY user_id ) t2";
    //投资金额
    public final static String invest_price= "select ifnull(a.price +b.price,0.00) from (select ifnull(sum(price),0.00) AS price from yjd_invest i, yjd_gift_register_bind g where i.user_id = g.user_id and i.addtime BETWEEN ? and ? ) a,( select ifnull(sum(price),0.00) as price from yjd_creditor_transfer c ,yjd_gift_register_bind g where c.buy_user_id = g.user_id and oktime BETWEEN ? and ? )b";
    //领取现金笔数
    public final static String receive_balance_count = "select count(*) from yjd_gift_receive where gift_balance >0 and start_time BETWEEN ? and ?";
    //领取现金金额
    public final static String receive_balance_price = "select ifnull(sum(gift_balance),0.00) from yjd_gift_receive where gift_balance >0 and start_time BETWEEN ? and ?";

    /**
     * 数据库信息服务
     */
    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private IBiDatabaseInfoService biDatabaseInfoService;

    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private IGiftReceiveService giftReceiveService;

    @RequestMapping("/index")
    public String index()throws Exception{
        request.setAttribute("start", "2013-12-21");
        request.setAttribute("now", BiDateUtil.initBiDate());
        return "giftreceive.index";
    }

    @RequestMapping("/summary")
    public void summary(@DateString String start, @DateString String end)throws  Exception{
        BiDatabaseInfoModel model = this.biDatabaseInfoService.getModelOne(FrameUtil.newHashMap("bs_type", 2),
                DbWREnums.READ);
        Long startDate = FrameUtil.getTime(DateUtil.parseDate(start,DateUtil.PATTERN_yyyyMMdd));
        Long endDate = FrameUtil.getTime(DateUtil.parseDate(end,DateUtil.PATTERN_yyyyMMdd))+86399;
        JdbcTemplate jt = BiUtil.getJdbcTemplate(model);//NumberUtil.formatNumber(model.getCash(), NumberUtil.FORMAT_4)
        int shareUserCount = jt.queryForObject(share_user_count,new Object[] {startDate, endDate},Integer.class);
        int receiveUserCount = jt.queryForObject(receive_user_count,new Object[] {startDate, endDate},Integer.class);
        int newMobileCount = jt.queryForObject(new_mobile_count,new Object[] {startDate, endDate},Integer.class);
        int registerUserCount = jt.queryForObject(register_user_count,new Object[] {startDate, endDate},Integer.class);
        int firstInvestCount = jt.queryForObject(first_invest_count,new Object[] {startDate, endDate},Integer.class);
        double fisrtInvestPrice = jt.queryForObject(fisrt_invest_price,new Object[] {startDate, endDate},Double.class);
        double investPrice = jt.queryForObject(invest_price,new Object[] {startDate, endDate,startDate, endDate},Double.class);
        int receiveBalanceCount = jt.queryForObject(receive_balance_count,new Object[] {startDate, endDate},Integer.class);
        double receiveBalancePrice = jt.queryForObject(receive_balance_price,new Object[] {startDate, endDate},Double.class);
        super.successData(FrameUtil.newHashMap("summaryVals", FrameUtil.newArrayList(
                NumberUtil.formatNumber(shareUserCount, NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(receiveUserCount, NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(newMobileCount, NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(registerUserCount, NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(firstInvestCount, NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(fisrtInvestPrice, NumberUtil.FORMAT_4),
                NumberUtil.formatNumber(investPrice, NumberUtil.FORMAT_4),
                NumberUtil.formatNumber(receiveBalanceCount, NumberUtil.FORMAT_2),
                NumberUtil.formatNumber(receiveBalancePrice, NumberUtil.FORMAT_4))));
    }

    /**
     * 统计
     *
     * @throws Exception
     *             void
     */
    @RequestMapping("count")
    public void investCount(@RequestParam(required = false) Integer timeType,
                            @DateString String timeFrom,
                            @RequestParam(required = false) String field) throws Exception {
        EChartsModel echartsMoel = giftReceiveService.getDates(
                TimeType.getType(timeType), Long.valueOf(timeFrom), field);
        super.successData(FrameUtil.newHashMap("option", echartsMoel));
    }


}
